package com.claire.jing.Controllers;

import com.claire.jing.Annocation.Anyone;
import com.claire.jing.Common.Result;
import com.claire.jing.Entity.Member;
import com.claire.jing.Service.MemberService;
import com.claire.jing.utils.ErrorMessUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Controller
public class AccountController {
    @Autowired
    private MemberService memberService;


    /**
     * 登陆页面
     * @param modelAndView
     * @return
     */
    @RequestMapping("/login")
    @Anyone
    public ModelAndView login(ModelAndView modelAndView){
        modelAndView.setViewName("login");
        return modelAndView;
    }

    /**
     * 去登陆
     * @param member
     * @param result
     * @return
     */
    @RequestMapping("/doLogin")
    @ResponseBody
    @Anyone
    public Result<String> doLogin(@RequestBody  @Validated Member member, BindingResult result, HttpServletRequest request){
        
       String message=null;
        if ((message = ErrorMessUtil.getErrorMess(result)) != null){
           return Result.fail(String.class,-1,message.toString());
        }

        //如果输入的合法，则去数据库查询
        List<Member> select = memberService.select(member);
        if (select.size()>0){
            //如果可以正常登陆，则将用户信息放入session
            HttpSession session = request.getSession(true);
            session.setAttribute("member",select.get(0));
            return   Result.success("");
        }
        return Result.fail(String.class,-1,"该用户不存在！");




    }

    /**
     * 用户个人信息页面
     * @param modelAndView
     * @return
     */
    @RequestMapping("/userInfo")
    public ModelAndView userInfo(ModelAndView modelAndView){
        modelAndView.setViewName("userInfo");
        return modelAndView;
    }

    /**
     * 去更新用户密码
     * @param member
     * @param result
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateUser")
    public Result<String> updateUser(HttpServletRequest request,@Validated Member member,BindingResult result){
        HttpSession session = request.getSession();
        Member pojo =(Member) session.getAttribute("member");
        int id = pojo.getId();
        member.setId(id);

        //先判断输入的信息是否合法
        String message=null;
        if ((message = ErrorMessUtil.getErrorMess(result)) != null){
            return Result.fail(String.class,-1,message.toString());
        }

        //如果输入信息合法，则去修改数据库
        int update = memberService.update(member);
        if (update!=0){
            //用户信息修改成功，将session删除(退出登录）
            session.removeAttribute("member");
            return Result.success("");//用户信息更新成功
        }
        return Result.fail(String.class,-1,"更新失败！");
    }

    /**
     * 退出登录
     * @param request
     * @param modelAndView
     * @return
     */
    @RequestMapping("/signOut")
    public ModelAndView signOut(HttpServletRequest request,ModelAndView modelAndView){
        HttpSession session = request.getSession();
        session.removeAttribute("member");
        modelAndView.setViewName("redirect:/login");
        return modelAndView;
    }


}
